import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/models/deviceinfomodel.dart';
import 'package:provider/provider.dart';
import 'package:flutter/cupertino.dart';

class TvPage2 extends StatefulWidget {
  const TvPage2({Key key}) : super(key: key);

  @override
  _TvPage2State createState() => _TvPage2State();
}

class _TvPage2State extends State<TvPage2> with SingleTickerProviderStateMixin{
  String path = 'assets/images/common/device/d-an.png';
  String path2 = 'assets/images/common/device/d-liang.png';

  Animation<double> animation;
  AnimationController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
        _controller = new AnimationController(
        duration: const Duration(milliseconds: 1000), vsync: this);
    animation = new Tween(begin: 20.0, end: 35.0).animate(_controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
        });
      });
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(255, 250, 212, 1),
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.transparent,
        leading: InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          child: Icon(
            Icons.keyboard_arrow_left,
            color: Color.fromRGBO(255, 255, 255, 1),
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            height: 300,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Positioned(
                    // top: animation.value,
                    top: animation.value,
                    child: Container(
                        child:
                            Image.asset('assets/images/common/device/台灯.png'))),
              ],
            ),
          ),
          Expanded(
            child: ListView(
              children: _devicelist(),
            ),
          ),
        ],
      ),
    );
  }

  _devicelist() {
    List<Widget> list = [];
    List devicelist = context.watch<DeviceInfomodel>().getlistbyType(5);
    for (int i = 0; i < devicelist.length; i++) {
      list.add(_buildleditem(devicelist[i]));
    }
    return list;
  }

  _buildleditem(Deviceitemmodel deviceitemmodel) {
    var path3 = 'assets/images/common/device/d-an.png';
    var work3 = '开启';
    var state = true;
    if (deviceitemmodel.content == 'isopen') {
      path3 = 'assets/images/common/device/d-liang.png';
      work3 = '开启';
      state = true;
    } else {
      path3 = 'assets/images/common/device/d-an.png';
      work3 = '关闭';
      state = false;
    }
    return Container(
      margin: EdgeInsets.fromLTRB(8, 8, 8, 8),
      width: 300,
      height: 100,
      decoration: new BoxDecoration(
        boxShadow: [
          BoxShadow(
              color: Color.fromRGBO(255, 255, 255, 0.3),
              offset: Offset(0.0, 0.0), //阴影xy轴偏移量
              blurRadius: 20.0, //阴影模糊程度
              spreadRadius: 0.5 //阴影扩散程度
              )
        ],
        color:  Color.fromRGBO(255, 255, 255, 1),
        //设置四周圆角 角度
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        //设置四周边框
        // border: new Border.all(width: 1, color: Colors.red),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Image.asset(path3),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                // widget.name,
                deviceitemmodel.name,
                style: TextStyle(color: Colors.black87, fontSize: 22),
              ),
              Row(
                children: [
                  Text(
                    '工作状态:',
                    style: TextStyle(color: Colors.black87, fontSize: 14),
                  ),
                  Text(
                    work3,
                    style: TextStyle(color: Colors.black54, fontSize: 14),
                  ),
                ],
              )
            ],
          ),
          CupertinoSwitch(
            trackColor:  Color.fromRGBO(255, 250, 212, 1),
            activeColor:  Colors.blueGrey[600],
            value: state,
            onChanged: (bool value) {
              print(value);
              if(value){
                 context.read<DeviceInfomodel>().operatedevice(deviceitemmodel.typeId, deviceitemmodel.deviceId, 'turnon');
              }
              else {
                context.read<DeviceInfomodel>().operatedevice(deviceitemmodel.typeId, deviceitemmodel.deviceId, 'turnoff');
              }
              setState(() {
                // this.state = value;
                // if (value) {
                //   this.state = 1;
                //   // _turnon();
                // } else {
                //   this.state = 0;
                //   _turnoff();
                // }

              });
            },
          ),
        ],
      ),
    );
  }
}
